#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
import RandomUtils

from base.Sequence import Sequence
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU
from gpgpu.Utils import LoadGPR64


# This test forces all GPRs to be loaded during the boot sequence. It also
# causes some floating point registers to be loaded as well, which will utilize
# GPRs during the boot sequence. The purpose is to ensure GPRs aren't reloaded
# with their final state values after being loaded with their initial state
# values by the StateTransition process. The final state values are set to
# invalid addresses in order to potentially trigger simulation failures when
# executing load and store instructions if the boot process does not work as
# expected.
class MainSequence(Sequence):
    def generate(self, **kargs):
        self.randomInitializeRegister("D0")
        for reg_index in range(1, 32):
            self.randomInitializeRegister("D%d" % reg_index)
            self.initializeRegister("x%d" % reg_index, RandomUtils.random64(0x0, 0x7FFFFFFFFFFF))

        if self.getGlobalState("AppRegisterWidth") == 32:
            instructions = ("LW##CALM", "SW##CALM")
        else:
            instructions = ("LD##CALM", "SD##CALM")

        for _ in range(RandomUtils.random32(100, 200)):
            self.genInstruction(self.choice(instructions), {"NoPreamble": 1})

        load_gpr64_seq = LoadGPR64(self.genThread)
        for reg_index in range(1, 32):
            load_gpr64_seq.load(
                reg_index,
                RandomUtils.random64(0x800000000000, 0x7FFFFFFFFFFFFFFF),
            )


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
